<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        let input = `()happy(xyz)new(wxy)year(t)`

        // input[i].charCodeAt()<='Z'.charCodeAt()&&input[i].charCodeAt()>='A'.charCodeAt()

        // 取所有同义字符
        let new_str = ''
        let text = []
        let tag = false
        let str = ''
        for (let i = 0; i < input.length; i++) {
            if (input[i] === '(') {
                tag = true
                continue
            } else if (input[i] === ')') {
                tag = false
                if (str.length) { text.push(str) }
                str = ''
                continue
            } else if (tag) {
                str += input[i]
            } else if (!tag) {
                new_str += input[i]
            }
        }

        let obj = {}
        let mp = new Set()
        let word = [text[0]]
        str = ''

        for (let i = 1; i < text.length; i++) {
            let T = true
            for (let j = 0; j < text[i].length; j++) {
                for (let k = 0; k < word.length; k++) {
                    if (word[k].toLowerCase().includes(text[i][j].toLowerCase())) {
                        word[k] += text[i]
                        T = false
                        break
                    }
                }
                if (!T) {
                    break
                }
            }
            if (T) {
                word.push(text[i])
                T = true
            }
        }

        let res = ''
        for (let i = 0; i < new_str.length; i++) {
            let T = true
            for (let j = 0; j < word.length; j++) {
                const minchar = [...word[j]].sort()[0]
                if (word[j].toLowerCase().includes(new_str[i].toLowerCase())) {
                    res += minchar
                    T = false
                    break
                }
            }
            if (T) {
                res += new_str[i]
            }

        }
        if(res.length){console.log(res);}else{
            console.log(0);
            
        }
        



    </script>
</body>

</html>